$package('YiYa.merchant');
YiYa.merchant = function(){
	var _box = null;
	var _this = {
		config:{
			event:{
				edit:function(){
					var selected = _box.utils.getCheckedRows();
					if ( _box.utils.checkSelectOne(selected)){
						var record = selected[0];
						if(record.checkStatus==1){
							//已入库，不得编辑
							YiYa.alert('警告','该账单已入库，不得编辑.','warning');
						}else{
							_box.handler.edit();
							var url = '../product/loadProdListByMerchant.do?merchantId='+record.merchant.id;
							$.get(url,function(data){
								$('#cc2').combogrid('grid').datagrid('loadData',data);
					        });
						}
					}
				},
				view:function(){
					var selected = _box.utils.getCheckedRows();
					if ( _box.utils.checkSelectOne(selected)){
						var record = selected[0];
						_box.handler.view();
						var url = '../product/loadProdListByMerchant.do?merchantId='+record.merchant.id;
						$.get(url,function(data){
							$('#cc4').combogrid('grid').datagrid('loadData',data);
				        });
					}
				}
			},
			dataGrid:{
				title:'进货明细列表',
				url:'dataList.do?type=1&product.type=1',
				columns:[[
				          {field:'id',checkbox:true},
				          {field:'number',title:'货单号',width:120,sortable:true},
				          /*
				          {field:'orderInfo_number',title:'订单编号',width:120,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["orderInfo"]).number;
						  }},
						  */
						  {field:'product_code',title:'产品编号',width:120,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["product"]).code;
						  }},
				          {field:'product_name',title:'产品名称',width:120,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["product"]).name;
						  }},
						  {field:'product_color',title:'产品颜色',width:80,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["product"]).color;
						  }},
						  {field:'product_standard',title:'产品规格',width:80,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["product"]).standard;
						  }},
						  {field:'merchant_name',title:'供应商',width:120,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["merchant"]).name;
						  }},
						  {field:'repository_name',title:'所在仓库',width:80,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["repository"]).name;
						  }},
				          {field:'quantity',title:'数量',width:120,sortable:true},
				          {field:'price',title:'单价',width:120,sortable:true},
						  {field:'bill_date',title:'账单日期',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  if(row["billDate"]!=null){
				        		  return new Date(row["billDate"].split(" ")[0].replace(/-/g,'/')).format("yyyy-MM-dd");
				        		  }
						  }},
						  {field:'checkStatus',title:'入库状态',width:80,
				        	  formatter:function(value,row,index){
					        		if(row.id!=null&&(value == 0||value == null)){
					        			return "未入库";
					        		}
					        		if(value == 1){
					        			return "已入库";
					        		}
					        	}  
				          },
						  {field:'create_by',title:'创建者',width:120,
				        	  formatter:function(value,row,index){
				        		  return new Object(row["createBy"]).nickName;
						  }},
						  {field:'remark',title:'备注',width:120,
							  formatter:function(value,row,index){
								  if(value!=undefined){
									  if(value.length>10){
										  var str = value.substring(0,8);
										  return str.concat('......');	  
									  }
									  return value;
								  }
								  
						  }}
				          ]],
				          toolbar:[
				                   {id:'btnadd',text:'添加',btnType:'add'},
				                   {id:'btnedit',text:'编辑',btnType:'edit'},
				                   {id:'btndelete',text:'删除',btnType:'remove'},
				                   {id:'btncheck',text:'确认入库',btnType:'check',iconCls:'icon-add',handler:function(){
				                	   var records = _box.utils.getCheckedRows();
				       				   if (_box.utils.checkSelect(records)){
					                	   $.messager.confirm('确认','确定要入库吗？入库后将无法更改！',function(r){  
					       				    if (r){  
					       				    	YiYa.progress();
					       				    	var arr = [],idKey = _this.config.dataGrid.idField || 'id'; //主键名称
										    	$.each(records,function(i,record){
										    		arr.push('id='+record[idKey]);
										    	});
										    	var data = arr.join("&");
										    	YiYa.deleteForm('../bill/check.do',data,function(result){
													YiYa.closeProgress();
													_box.handler.refresh();
												});
					       				    }  
					       				   });
				       				   }
				                   }},
				                   {id:'btnview',text:'查看',btnType:'view'}
				                   ]
			}
		},
		init:function(){
			$('.easyui-datebox').datebox({
			    formatter: function(date){ return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();},
			    parser: function(date){ return new Date(Date.parse(date.replace(/-/g,"/")));}
			});
			$('#cc1').combobox({
			    onChange:function(newValue,oldValue){
			    	$("#cc2").combogrid('clear');
			    }
			});
			$('#cc2').combogrid({
			    onSelect:function(newValue,oldValue){
			    	$.get("../price_rel/getByMhtAndProd.do?productId="+newValue+"&merchantId="+$('input[name="merchantId"]').val(),function(data,status){
			    		if(data.data!=null){
				    		$("#price").val(parseFloat(data.data.price));
				    		$("input[name='price']").val(data.data.price);
			    		}
			    	});
			    }
			});
			$("#export-btn").click(function(){
				var url = "../bill/export_out_bill/1.do"
				document.searchForm.action = url;
				document.searchForm.submit();
			});
			_box = new YDataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function(){
	YiYa.merchant.init();
});


